package com.yans.managers
{
	import com.maclema.mysql.Connection;
	import com.maclema.mysql.MySqlToken;
	import com.maclema.mysql.ResultSet;
	import com.maclema.mysql.Statement;
	import com.yans.interfaces.IFish;
	import com.yans.interfaces.ISQLLiteManager;
	import com.yans.interfaces.vo.ISQLConnectionInfo;
	import com.yans.managers.vo.SQLConnectionInfo;
	
	import flash.events.Event;
	
	import mx.rpc.AsyncResponder;
	
	public class SQLManager implements ISQLLiteManager, IFish
	{
		private var info:ISQLConnectionInfo;
		private var con:Connection;
		
		public function SQLManager()
		{
		}
		
		private var _connectedFunc:Function;
		public function open(path:String, callback:Function):void {
			if(null == info) return;
			_connectedFunc = callback;
			con = new Connection(info.host, info.port, info.username, info.password, info.database);
			con.addEventListener(Event.CONNECT, onConnected);
			con.connect();
		}
		
		public function getConnectInfo(host:String="", port:int=0, username:String="", password:String="", database:String=""):ISQLConnectionInfo {
			return new SQLConnectionInfo(host, port, username, password, database) as ISQLConnectionInfo;
		}
		
		
		private function onConnected(e:Event):void {
			trace("数据库连接成功！");
			_connectedFunc();
		}
		
		public function create(tableName:String, columns:String, callback:Function):void {
			
		}
		
		private var st:Statement, token:MySqlToken;
		public function execute(sql:String, callback:Function):void {
			st = con.createStatement();
			token = st.executeQuery(sql);
			token.addResponder(new AsyncResponder(
				//callback,		//function(data:Object, token:Object):void
				function(result:ResultSet, token:MySqlToken):void {
					callback(result);
				},
				function(info:Object, token:Object):void {
					trace("#Error" + info);
				},
				token
			));
		}
		
		public function init(data:Object=null):void {
			info = data as ISQLConnectionInfo;
			_quque = new Vector.<MySqlToken>;
		}
		
		public function dispose():void {
		}
		
		private var _quque:Vector.<MySqlToken>;
		public function swim(times:int, dt:Number=0.04):void {
			
		}
	}
}